package com.cyq.day21.managementsystem;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.time.LocalDate;

public class EmployeeManagement extends JFrame {
    private JPanel panel;
    private JTextArea textArea;
    private JTextField textField;
    private JButton button;
    String data = "";
    Order[] orders = new Order[50];

    public EmployeeManagement(Employee[] employees, int index) {
        orders[0] = new Order(1, "张三", "12345", "2020-03-15", "", "");
        orders[1] = new Order(2, "赵四", "54321", "2020-03-16", "", "");
        orders[2] = new Order(3, "王五", "123456", "2020-03-17", "", "");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setTitle("员工管理");
        textArea = new JTextArea();
        textArea.setText("欢迎使用员工管理" + "\n"
                + "请输入选项：" + "\n"
                + "1.入库" + "\n"
                + "2.客户取货" + "\n"
                + "3.查看未收货订单" + "\n"
                + "4.按取货吗查询订单" + "\n"
                + "5.按姓名查询订单" + "\n"
                + "6.修改密码" + "\n"
                + "7.安全退出" + "\n");
        add(textArea);
        panel = new JPanel();
        textField = new JTextField(20);
        button = new JButton("确定");
        panel.add(textField);
        panel.add(button);
        add(BorderLayout.SOUTH, panel);
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if ("".equals(textField.getText())) {
                    JOptionPane.showMessageDialog(null, "请输入正确的选项");
                }
                //入库
                if ("1".equals(textField.getText())) {
                    OrderManagement orderManagement = new OrderManagement(orders);
                    orderManagement.setVisible(true);
                }
                //客户取货
                if ("2".equals(textField.getText())) {
                    boolean flag = false;
                    String inputnum = JOptionPane.showInputDialog(null, "请输入取货码");
                    for (int i = 0; i < orders.length; i++) {
                        if (orders[i] != null) {
                            if (Integer.parseInt(inputnum) == orders[i].getCode()) {
                                if ("".equals(orders[i].getTakename())) {
                                    data += orders[i].getName() + " " + orders[i].getCode() + " " + orders[i].getTel() + " " + orders[i].getTakeTime() + " " + orders[i].getLoginTime() + "\n";
                                    int inputCheckNum = JOptionPane.showConfirmDialog(null, "收货人" + " " + "收货码" + " " + "收货人电话" + " " + "取货时间" + " " + "入库时间" + "\n" + data);
                                    data = "";
                                    if (inputCheckNum == 0) {
                                        String inputname = JOptionPane.showInputDialog(null, "请输入取货人");
                                        orders[i].setTakename(inputname);
                                        orders[i].setTakeTime(LocalDate.now().toString());
                                        JOptionPane.showMessageDialog(null, "取货成功");
                                        flag = true;
                                    } else {
                                        return;
                                    }
                                } else {
                                    JOptionPane.showMessageDialog(null, "该订单已经被取走");
                                    return;
                                }
                            }
                        }
                    }
                    if (!flag) {
                        JOptionPane.showMessageDialog(null, "该取货码不存在");
                    }
                }
                //查看未收货订单
                if ("3".equals(textField.getText())) {
                    for (int i = 0; i < orders.length; i++) {
                        if (orders[i] != null) {
                            if ("".equals(orders[i].getTakeTime())) {
                                data += orders[i].getName() + " " + orders[i].getCode() + " " + orders[i].getTel() + " " + orders[i].getTakeTime() + " " + orders[i].getLoginTime() + "\n";
                            }
                        }
                    }
                    JOptionPane.showMessageDialog(null, "收货人" + " " + "收货码" + " " + "收货人电话" + " " + "取货时间" + " " + "入库时间" + "\n"
                            + data);
                    data = "";
                }
                //按取货码查询订单
                if ("4".equals(textField.getText())) {
                    boolean flag = false;
                    String inputnum = JOptionPane.showInputDialog(null, "请输入取货码");
                    for (int i = 0; i < orders.length; i++) {
                        if (orders[i] != null) {
                            if (Integer.parseInt(inputnum) == orders[i].getCode()) {
                                data += orders[i].getName() + " " + orders[i].getCode() + " " + orders[i].getTel() + " " + orders[i].getTakeTime() + " " + orders[i].getLoginTime() + orders[i].getTakename() + "\n";
                                flag = true;
                            }
                        }
                    }
                    if (!flag) {
                        JOptionPane.showMessageDialog(null, "该取货码不存在");
                    } else {
                        JOptionPane.showMessageDialog(null, "收货人" + " " + "收货码" + " " + "收货人电话" + " " + "取货时间" + " " + "入库时间" + " " + "取货人" + "\n"
                                + data);
                        data = "";
                    }
                }
                //按姓名查询订单
                if ("5".equals(textField.getText())) {
                    String inputname = JOptionPane.showInputDialog(null, "请输入取货人姓名");
                    for (int i = 0; i < orders.length; i++) {
                        if (orders[i] != null) {
                            if (inputname.equals(orders[i].getTakename())) {
                                data += orders[i].getName() + " " + orders[i].getCode() + " " + orders[i].getTel() + " " + orders[i].getTakeTime() + " " + orders[i].getLoginTime() + orders[i].getTakename() + "\n";
                            }
                        }
                    }
                    JOptionPane.showMessageDialog(null, "收货人" + " " + "收货码" + " " + "收货人电话" + " " + "取货时间" + " " + "入库时间" + " " + "取货人" + "\n"
                            + data);
                    data = "";
                }
                //修改密码
                if ("6".equals(textField.getText())) {
                    String inputpwd = JOptionPane.showInputDialog(null, "请输入旧密码");
                    if (inputpwd.equals(employees[index].getPassword())) {
                        String inputpwd1 = JOptionPane.showInputDialog(null, "请输入新密码");
                        String inputpwd2 = JOptionPane.showInputDialog(null, "请输入新密码");
                        if (inputpwd1.equals(inputpwd2)) {
                            employees[index].setPassword(inputpwd1);
                            JOptionPane.showMessageDialog(null, "密码修改成功");
                        } else {
                            JOptionPane.showMessageDialog(null, "两次密码不一致");
                        }
                    } else {
                        JOptionPane.showMessageDialog(null, "旧密码错误");
                    }
                }
                //安全退出
                if ("7".equals(textField.getText())) {
                    ChooseFrame chooseFrame = new ChooseFrame();
                    chooseFrame.setVisible(true);
                    EmployeeManagement.this.dispose();
                }
            }
        });

    }
}
